<?php
namespace app\manage\controller;

use app\manage\model\{da} as {da}model;
use app\manage\controller\BaseController;
use think\facade\Db;
use think\facade\View;
class {da} extends BaseController
{

    public function index()
    {
    	${xiao}=new {da}model();
    	$key=input('key','');
    	View::assign('key',$key);
		
		${xiao}=${xiao}{con_cha}->paginate(10,false,['query'=>request()->param()])->each(function($item, $key){
			//$item->nickname = 'think';
		});
		View::assign('{xiao}',${xiao});
		View::assign('count1', ${xiao}->total());
       	return View::fetch();
    }
	public function ajax()
    {
    	$data=input('param.');
		${xiao}=new {da}model();
		if($data['type']=='{xiao}_del'){
			$info=${xiao}->destroy($data['id']);
			if($info){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='{xiao}_sort'){
			$arrlength=count($data['id']);
			$ar=[];
			for($x=0;$x<$arrlength;$x++)
			{
			    $ar[$x]=['id'=>$data['id'][$x], 'sort'=>$data['sort'][$x]];
			}
			$info=${xiao}->saveAll($ar);
			if($info){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='{xiao}_start'){
		    if(${xiao}->where('id',$data['id'])->update(['isopen'=>1])){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='{xiao}_stop'){
		    if(${xiao}->where('id',$data['id'])->update(['isopen'=>0])){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		return json(['code' => 0, 'message' => '失败']);
    }
	public function add()
    {
    	if(request()->isPost()){
			$data=input('post.');
			$validate = new \app\manage\validate\{da};
			if(!$validate->check($data)){
				$this->error($validate->getError());
			}	
			{file}
            {date_zhuan}
			${xiao}=new {da}model();
			${xiao}->data($data);
			$res=${xiao}->save();
			if($res){
				$this->success('添加成功',url('{xiao}/index',['st'=>1]));
			}else{
				$this->error('添加失败了');
			}
    	}
       return View::fetch();
    }
	public function edit()
    {
    	${xiao} = new {da}model();
    	if(request()->isPost()){
    		$data=input('post.');
			$validate = new \app\manage\validate\{da};
			if(!$validate->check($data)){
				$this->error($validate->getError());
			}	
			{edit_file}
            {date_zhuan}
			$res=${xiao}->update($data,['id' => input('id')]);
			if($res){
				$this->success('修改成功',url('{xiao}/index',['st'=>1]));
			}else{
				$this->error('修改失败了');
			}
    	}
		
		$id=input('id');
		$data= ${xiao}->find($id);
		View::assign('data',$data);
       return View::fetch();
    }
}
